#ifndef WXAPI_H
#define WXAPI_H

#include <QFile>
#include <QMediaPlayer>
#include <QNetworkAccessManager>
#include <QObject>
#include <QTimer>

class wxapi : public QObject
{
    Q_OBJECT
public:
    explicit wxapi(QObject *parent = nullptr);
    ~wxapi();
    void doubao_networkRequestInterface(QString, QByteArray);
    void baidu_networkRequestInterface(QString, QByteArray);
    void init_audio_play();

private:
    QString get_token_module = "https://aip.baidubce.com/oauth/2.0/token?grant_type=%1&client_id=%2&client_secret=%3";
    QString get_audio_token_url;
    QString audio_access_token;
    QString grant_type = "client_credentials";
    QString audio_client_id = "LyVfPVT7KI1bD79NwJ5GZLq3";
    QString audio_client_secret = "k04M39Nnj57lSwk10p2BWxuS64t8eK3d";
    QString audio_rec_url = "https://vop.baidu.com/server_api";
    QString doubao_text;

    QString audioMix_createTask_url_module = "https://aip.baidubce.com/rpc/2.0/tts/v1/create?access_token=%1";
    QString audioMix_QueryTask_url_module = "https://aip.baidubce.com/rpc/2.0/tts/v1/query?access_token=%1";
    QString audioMix_createTask_url;
    QString audioMix_QueryTask_url;
    QString task_id;
    QString audio_url;

    QString post_doubao_url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
    QString api_key = "756e3464-d520-4c1b-8c87-3d44dfb29b7a";
    //QString mode_id = "ep-20250313161447-nsq72";
    QString mode_id = "ep-20250317192902-xnsc4";

    QNetworkAccessManager *manager;

    QString get_access_token_interface(QByteArray, QString);
    QString get_doubao_reply_interface(QByteArray, QString, QString, QString);
    QString get_pcm_text_interface(QByteArray, QString);
    QString get_query_reply_interface(QByteArray, QString, QString, QString);
    QByteArray pcmData;

    QMediaPlayer *audio_play;


    QFile file;
    QTimer timer;
    int count;
    bool abort_sig;

public slots:
    void readyReadInterface();
    void doubao_finished();
    void baidu_finished();
    void post_content_to_doubao(QString);
    void post_pcmData_to_baidu(QByteArray);
    void post_text_to_baidu_mix1(QString);
    void post_text_to_baidu_mix2();
    void download_audio_from_url(QString);
    void deal_audio_play(QMediaPlayer::MediaStatus);
    void get_speech_data_finished();
    void onTimeout();

signals:
    void OnScreenDisSig(QString);
    void send_text_to_doubao_sig(QString);
    void text_convert_audio_sig(QString);
    void mix_task_query_sig();
    void download_audio_sig(QString);
    void reply_abort_sig();
};

#endif // WXAPI_H
